package com.gogowan.agroenterprise.widget.xlistview;

import android.app.Activity;
import android.app.Dialog;
import android.content.Context;
import android.text.Editable;
import android.text.TextWatcher;
import android.util.DisplayMetrics;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.view.inputmethod.InputMethodManager;
import android.widget.AdapterView;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;

import com.gogowan.agroenterprise.R;
import java.util.List;


public class BottomTongYongDialog implements View.OnClickListener {
    private final String mTitle;
    private View inflate;
    private Context mContext;
    private ImageView ib_colse;
    private ListView listView;
    private List<WorkStateBean> mList;
    private Dialog alertDialog;
    private DialogTextCHangedLinstner dialogTextCHangedLinstner;
    private DialogListItemAdapter itemAdapter;
    private EditText et_serch;
//    private AVLoadingIndicatorView avi;

    public BottomTongYongDialog(List<WorkStateBean> list, String title){
        mList=list;
        mTitle=title;
    }
    public void showDialog(Context context) {
        View view = View.inflate(context, R.layout.view_dialog_list, null);
        alertDialog=new Dialog(context);
//        AlertDialog.Builder builder = new AlertDialog.Builder(context);
//        alertDialog = builder.show();
        Window window = alertDialog.getWindow();
        alertDialog.getWindow().requestFeature(Window.FEATURE_NO_TITLE);
        WindowManager.LayoutParams layoutParams = new WindowManager.LayoutParams();
        layoutParams.copyFrom(alertDialog.getWindow().getAttributes());
        layoutParams.width = WindowManager.LayoutParams.WRAP_CONTENT;
        DisplayMetrics displayMetrics = new DisplayMetrics();
        ((Activity)context).getWindowManager().getDefaultDisplay().getMetrics(displayMetrics);
        int screenHeight = displayMetrics.heightPixels;
        layoutParams.height = (screenHeight/3)*2; // 设置固定高度为500像素
        window.setAttributes(layoutParams);
        window.setContentView(view);
        window.setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_RESIZE);
        alertDialog.setCancelable(true);
        itemAdapter = new DialogListItemAdapter(context);
        itemAdapter.setList(mList);

        TextView titleView = (TextView) view.findViewById(R.id.textview_alert_title);
        titleView.setText(mTitle);

        et_serch = (EditText) view.findViewById(R.id.et_serch);
        et_serch.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {

            }

            @Override
            public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {

            }

            @Override
            public void afterTextChanged(Editable editable) {
                String text = et_serch.getText().toString();
                dialogTextCHangedLinstner.textChanged(text);
            }
        });
        ListView listView = (ListView) view.findViewById(R.id.textview_alert_text);
        listView.setAdapter(itemAdapter);

        listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                alertDialog.dismiss();
                InputMethodManager imm = (InputMethodManager) ((Activity)context).getSystemService(Context.INPUT_METHOD_SERVICE);
                imm.hideSoftInputFromWindow(et_serch.getWindowToken(), 0);
                mdialogLinstner.closeLinstner(mList.get(position));
                itemAdapter.notifyDataSetChanged();
            }
        });
        alertDialog.show();
    }

    public void setOnclickLisner(DialogCloseLinstner linstner) {
        mdialogLinstner=linstner;
    }
    private DialogCloseLinstner mdialogLinstner;
    @Override
    public void onClick(View view) {
    }


    public void setTextChangedLisner(DialogTextCHangedLinstner dialogTextCHangedLinstner) {
        this.dialogTextCHangedLinstner =dialogTextCHangedLinstner;
    }

    public void setDialogAdapterChanged(List<WorkStateBean> list) {
        mList=list;
        itemAdapter.setList(mList);
        itemAdapter.notifyDataSetChanged();
    }
}
